package cn.usian.jyzx.pay.controller;

import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.jyzx.pay.service.AliPayService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 11 11 14 02
 **/

@RestController
@Slf4j
public class AliPayController {


    @Autowired
    AliPayService aliPayService;


    /**
     * /api/order/alipay/submitAlipay/1855852440973348864
     *
     */
    @GetMapping("/api/order/alipay/submitAlipay/{orderNo}")
    public Result submitAlipay(@PathVariable String orderNo) throws AlipayApiException {

        String s = aliPayService.submitAlipay(orderNo);
        return Result.build(s, ResultCodeEnum.SUCCESS);
    }

    /**
     * ？name=zhangsan&age=14
     * @param request
     * @return
     */

    @GetMapping("/api/order/alipay/returnAlipay")
    public String returnAlipay(HttpServletRequest request) throws AlipayApiException {

        log.info("======returnAlipay============");

        //获取支付宝POST过来反馈信息，将异步通知中收到的待验证所有参数都存放到map中
        //key：参数
        //value：值
        Map< String , String > params = new HashMap< String , String >();
        Map requestParams = request.getParameterMap();
        for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();){
            String name = (String)iter.next();
            String[] values = (String [])requestParams.get(name);
            String valueStr = "";
            for(int i = 0;i < values.length;i ++ ){
                valueStr =  (i==values.length-1)?valueStr + values [i]:valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put (name,valueStr);
        }

        log.info("获取支付宝POST过来反馈信息:{}",params);
        String resMessage = aliPayService.notifyPay(params);


        return resMessage;
    }


    @GetMapping("/api/order/alipay/notifyAlipay")
    public String notifyAlipay(HttpServletRequest request) throws AlipayApiException {

        log.info("======notifyAlipay============");

        //获取支付宝POST过来反馈信息，将异步通知中收到的待验证所有参数都存放到map中
        //key：参数
        //value：值
        Map< String , String > params = new HashMap< String , String >();
        Map requestParams = request.getParameterMap();
        for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();){
            String name = (String)iter.next();
            String[] values = (String [])requestParams.get(name);
            String valueStr = "";
            for(int i = 0;i < values.length;i ++ ){
                valueStr =  (i==values.length-1)?valueStr + values [i]:valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put (name,valueStr);
        }

        log.info("获取支付宝POST过来反馈信息:{}",params);
        String resMessage = aliPayService.notifyPay(params);


        return resMessage;
    }
}
